package com.ilink.ilinkuser.controller;



import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.log.method.MethodLog;
import com.ilink.ilinkcommon.utils.UserContext;
import com.ilink.ilinkuser.DTO.UserApiKeyUpdateAndDeleteRequest;
import com.ilink.ilinkuser.DTO.UserApiKeysRequest;

import com.ilink.ilinkuser.domain.VO.FieldsResponse;
import com.ilink.ilinkuser.entity.Model;
import com.ilink.ilinkuser.entity.ModelApiKey;


import com.ilink.ilinkuser.service.UserApiKeysService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user/personal_space")
public class UserApiKeysController {

    @Autowired
    private UserApiKeysService userApiKeysService;
    @Autowired
    private ObjectMapper objectMapper;

    @MethodLog("返回所有模型信息")
    @GetMapping("/api/models")
    public R getAllModels(){
        try{
            List<Model>  models=userApiKeysService.findAllModels();
            if(models!=null&&models.size()>0){
                return R.ok("查询到模型数据",models);
            }
            else
                return R.error("获取模型基本信息失败");
        }
        catch (Exception e){
           return  R.error("获取模型基本信息失败,由于",e.getMessage());
        }

    }
    @MethodLog("搜索模型")
    @GetMapping("/api/models/search")
    public R getModelsByName(@RequestParam String name){
        try{
            List<Model> models=userApiKeysService.getModelsByName(name);
            if(models!=null&&models.size()>0){
                return R.ok("查询到模型数据",models);
            }
            else
                return R.error("获取模型基本信息失败");
        }
        catch (Exception e){

            return R.error("获取模型基本信息失败，由于",e.getMessage());
        }

    }
    @MethodLog("返回指定模型的所有字段")
    @GetMapping("/api/models/fields")
    public R getAllFields(@RequestParam Long modelId){
        try{
            ModelApiKey modelApiKey =userApiKeysService.getFieldsByModelId(modelId);
            ObjectMapper objectMapper = new ObjectMapper();
            List<Map<String, Object>> fields = objectMapper.readValue(
                    modelApiKey.getFields(),
                    new TypeReference<List<Map<String, Object>>>() {}
            );

            FieldsResponse fieldsResponse = new FieldsResponse();
            fieldsResponse.setFields(fields);
            return R.ok("成功返回模型字段",fieldsResponse);
        }
        catch (Exception e){
            return R.error("未获取到该模型全部字段,由于",e.getMessage());
        }
    }

    @MethodLog("返回用户在该模型下的所有apikeys")
    @GetMapping("/api/model/apikeys")
    public R getAllApikeys(@RequestParam Long modelId){
        String userId = UserContext.getUserId();
        try{
            List<Map<String,Object>> userApiKeys=userApiKeysService.getUserApiKeysByUserIdAndModelId(userId,modelId);
            if(userApiKeys!=null){
                return R.ok("获取到用户在该模型下的所有apikeys",userApiKeys);

            }
            return R.error("未获取到apikeys");
        }
        catch(Exception e){
            return R.error("未获取到apikeys,由于",e.getMessage());
        }
    }

    @MethodLog("新增apikey")
    @PostMapping("/api/model/addapikey")
    public R saveApikey(@RequestBody UserApiKeysRequest userApiKeysRequest){
        try{
            boolean isSaved=userApiKeysService.saveUserApiKey(userApiKeysRequest);
            if(isSaved){
                return R.ok("成功增加apikey");
            }
            else
                return R.error("添加apikey失败");
        }
        catch(Exception e){
            return R.error("添加apikey失败，由于",e.getMessage());
        }
    }

    @MethodLog("修改apikey")
    @PostMapping("/api/model/updateapikey")
    public R updateApikey(@RequestBody UserApiKeyUpdateAndDeleteRequest userApiKey){
        try{
            boolean isUpdate=userApiKeysService.updateUserApiKey(userApiKey);
            if(isUpdate){
                return R.ok("成功修改apikey");
            }
            else
            {
                return R.error("未能修改apikey");
            }
        }
        catch(Exception e){
            return R.error("未能修改apikey，由于",e.getMessage());
        }
    }

    @MethodLog("删除apikey")
    @PostMapping("/api/model/deleteapikey")
    public R deleteApikey(@RequestBody UserApiKeyUpdateAndDeleteRequest userApiKey){
        try{
            boolean isDelete=userApiKeysService.deleteUserApiKey(userApiKey);
            if(isDelete){
                return R.ok("成功删除apikey");
            }
            else {
                return R.error("未能删除apikey");
            }
        }
        catch(Exception e){
            return R.error("未能删除apikey，由于",e.getMessage());
        }
    }


}
